package edu.bbu.license.client.android.services.transfer.model;

import java.text.DecimalFormat;

import edu.bbu.license.client.android.communication.utils.FileTransferObserver;
import edu.bbu.license.client.android.gui.transfer.model.FileTransferItem;

public class SimpleTransferObject implements FileTransferObserver {

	private int status;
	private int recentStatus;
	
	private long fileSize = 0;
	private String fileName = "";
	private int progressPercent = 0;
	
	private FileTransferItem fileTransferItem;
	private final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
	
	public void setFileTransferItem(FileTransferItem fileTransferItem) {
		this.fileTransferItem = fileTransferItem;
		
		this.fileTransferItem.setFileName(fileName);
		this.fileTransferItem.setFileSize(getReadableFileSize(fileSize));
		this.fileTransferItem.setProgressPercent(progressPercent);
		
		if(status == UPLOAD) {
			this.fileTransferItem.setUploadStatus();
		} else if (status == DOWNLOAD) {
			this.fileTransferItem.setDownloadStatus();
		} else if (status == PAUSED) {
			this.fileTransferItem.setPausedStatus();
		} else if (status == STOPPED) {
			this.fileTransferItem.setStoppedStatus();
		} else if (status == FINISHED) {
			this.fileTransferItem.setFinishedStatus();
		} else {
			this.fileTransferItem.setCrashedStatus();
		}
	}
	
	private String getReadableFileSize(long size) 
	{
		if (size <= 0)
		{
			return "0 B";
		}
		
		int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
		return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
	}
	
	@Override
	public void setFileName(String fileName) {
		this.fileName = fileName;
		
		if(fileTransferItem != null) {
			this.fileTransferItem.setFileName(fileName);
		}
	}

	@Override
	public String getFileName() {
		return fileName;
	}

	@Override
	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
		
		if(fileTransferItem != null) {
			this.fileTransferItem.setFileSize(Long.toString(fileSize));
		}
	}

	@Override
	public long getFileSize() {
		return fileSize;
	}

	@Override
	public void setProgressPercent(int percent) {
		this.progressPercent = percent;
		
		if(fileTransferItem != null) {
			this.fileTransferItem.setProgressPercent(progressPercent);
		}
	}

	@Override
	public int getProgressPercent() {
		return progressPercent;
	}

	@Override
	public synchronized void setStatus(int status) {
		this.status = status;	
		
		if(fileTransferItem != null) {
			if(status == UPLOAD) {
				this.fileTransferItem.setUploadStatus();
			} else if (status == DOWNLOAD) {
				this.fileTransferItem.setDownloadStatus();
			} else if (status == PAUSED) {
				this.fileTransferItem.setPausedStatus();
			} else if (status == STOPPED) {
				this.fileTransferItem.setStoppedStatus();
			} else if (status == FINISHED) {
				this.fileTransferItem.setFinishedStatus();
			} else {
				this.fileTransferItem.setCrashedStatus();
			}
		}
	}

	@Override
	public synchronized int getStatus() {
		return status;
	}

	@Override
	public synchronized void startTransfer() {
		if(status == PAUSED)
		{
			setStatus(recentStatus);
			this.notify();
			
		}
	}

	@Override
	public synchronized void pauseTransfer() {
		if(status == UPLOAD || status == DOWNLOAD)
		{
			recentStatus = status;
			setStatus(PAUSED);
		}
	}

	@Override
	public synchronized void stopTransfer() {
		if(status == UPLOAD || status == DOWNLOAD)
		{
			setStatus(STOPPED);
		}
		else if(status == PAUSED)
		{
			setStatus(STOPPED);
			this.notify();
		}
	}
}
